package org.acghub.config;

import lombok.extern.slf4j.Slf4j;

import java.util.*;

/**
 * @Desc
 * @Author miyuki4j
 * @Date 2025/7/26/星期六 14:48
 * @Version 1.0
 */
@Slf4j
public class ConfManager {


    private static final Map<String, List<TableDataImporter.ImportData>> IMPORT_DATAS_MAP = new HashMap<>();

    private static final Map<Class<? extends Conf>, NavigableMap<? extends Comparable, Conf>> CONF_MAP = new HashMap<>();

    public static void importData() throws Exception {
        List<TableDataImporter.ImportData> importDatas = TableUtil.importDatas();
        for (TableDataImporter.ImportData importData : importDatas) {
            IMPORT_DATAS_MAP.computeIfAbsent(importData.getFullClassName(), k -> new ArrayList<>()).addAll(importDatas);
        }
    }

    public static void loadConf() throws Exception {
        Map<Class<? extends Conf>, List<Conf>> confs = TableUtil.loadConfs();
        for (Map.Entry<Class<? extends Conf>, List<Conf>> entry : confs.entrySet()) {
            NavigableMap<Comparable, Conf> confMap = new TreeMap<>();
            for (Conf conf : entry.getValue()) {
                Comparable id = conf.getId();
                confMap.put(id, conf);
            }
            CONF_MAP.put(entry.getKey(), confMap);
        }
    }

    public static List<TableDataImporter.ImportData> getImportDatas(String fullClassName) {
        return IMPORT_DATAS_MAP.get(fullClassName);
    }

    public static <T extends Conf> T getConf(Class<T> clazz, Comparable id) {
        NavigableMap<? extends Comparable, Conf> confMap = CONF_MAP.get(clazz);
        if (confMap == null) {
            return null;
        }
        return (T) confMap.get(id);
    }

    public static <T extends Conf> NavigableMap<?, T> getConfs(Class<T> tClazz) {
        NavigableMap<? extends Comparable, Conf> confMap = CONF_MAP.get(tClazz);
        if (confMap == null) {
            return null;
        }
        return (NavigableMap<? extends Comparable, T>) confMap;
    }

    public static <ID extends Comparable, T extends Conf> NavigableMap<ID, T> getConfs(Class<ID> __, Class<T> tClazz) {
        NavigableMap<? extends Comparable, Conf> confMap = CONF_MAP.get(tClazz);
        if (confMap == null) {
            return null;
        }
        return (NavigableMap<ID, T>) confMap;
    }

}
